#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;

int main()
{
    pid_t pid;
    int status;
    pid = fork();
    if (pid == 0)
    {
        // child process
        int cnt = 6;
        while (cnt--)
        {
            printf("child process,PID: %d\n",getpid());
            sleep(1);
        }
        exit(0);
    }
    // father process
    while (1)
    {
        printf("father process\n");
        sleep(1);
        pid_t wpid = waitpid(pid, &status, WNOHANG);
        if (wpid == pid)
        {
            printf("child process exit, exit code:%d , signal code:%d\n",
                   WEXITSTATUS(status),
                   WTERMSIG(status)); // WEXITSTATUS(status) 获取子进程的退出码
            break;
        }
    }

    return 0;
}